﻿<%@ WebHandler Language="C#" Class="RTGS.Web.ajaxhandler.spt_record_detail_i" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using RTGS.Library.Enum;
using RTGS.Library.DataAccess.Rtgs;
using RTGS.Library.Entities.Rtgs;
using RTGS.Web.support;

using System.Configuration;

namespace RTGS.Web.ajaxhandler
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>

public class spt_record_detail_i : BaseHandler
    {

        private stpRecordDetailIRepo strRecordDal = new stpRecordDetailIRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
        //private sysTLogRepo logDal = new sysTLogRepo(ConfigurationManager.ConnectionStrings["Kemenhub"].ConnectionString);
        
    
    
        private string MethodName = string.Empty;
        private string StatusName = string.Empty;

        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            MethodName = context.Request.Params["method"];
            StatusName = context.Request.Params["status"];
            context.Response.Clear();
            context.Response.Buffer = true;

            switch (MethodName.ToLower())
            {
                
                case ("getdata"):
                    context.Response.Write(GetData(context));
                    break;
                default:
                    // do nothing
                    break;
            }

            context.Response.Flush();
            context.Response.End();

        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #region CRUD Handler

       

        private string GetData(HttpContext context)
        {
            int page = 1;
            int.TryParse(context.Request.QueryString["page"] ?? "1", out page);

            int pageSize = 10;
            int.TryParse(context.Request.QueryString["rows"] ?? "10", out pageSize);

            string sidx = ((context.Request.QueryString["sidx"]) == null ? stpRecordDetailI.Fields.recordDetailItrRecordtype: context.Request.QueryString["sidx"].ToString());

            string sord = ((context.Request.QueryString["sord"]) == null ? "ASC" : context.Request.QueryString["sord"].ToString());

            string field = ((context.Request.Params["field"]) == null ? string.Empty : context.Request.QueryString["field"].ToString());
            string operand = ((context.Request.Params["operand"]) == null ? string.Empty : context.Request.QueryString["operand"].ToString());
            string value = ((context.Request.Params["value"]) == null ? string.Empty : context.Request.QueryString["value"].ToString());

            string parameters = string.Empty;
            
            //if(context.Request.Params["status"].ToString().Trim()=="repair")
              //  parameters = " WHERE 

            if (!string.IsNullOrEmpty(value))
            {
                field = operand.ToLower().Contains("like") ? "LOWER(" + field + ")" : field;
                field = field.ToLower().Contains("date") ? "CONVERT(char(8), " + field + ", 112)" : field;

                var filter = operand.ToLower().Contains("like") ? "'%" + value.ToLower() + "%'" :
                    field.ToLower().Contains("date") ? "'" + Helper.ToSQLServerDateISO(value) + "'" : "'" + value + "'"; // TextBox Filter

                parameters = " WHERE " + field + " " + operand + " " + filter;
            }

            int totalCount = Convert.ToInt32(strRecordDal.SelectScalar(SQL.Function.Aggregate.Count, stpRecordDetailI.Fields.recordDetailItrRecordtype, parameters));

            decimal dc = decimal.Ceiling(Convert.ToDecimal(totalCount) / Convert.ToDecimal(pageSize));
            int totalPages = Convert.ToInt32(dc);

            List<stpRecordDetailI> pagedList = strRecordDal.GetList(pageSize, page, sidx, sord, parameters);

            if (!string.IsNullOrEmpty(strRecordDal.GetErrorMessage()))
            {
                //sysTLog log = new sysTLog();
                //log.logEvent = "GetData";
                //log.logMessage = userDal.GetErrorMessage();
                //log.logSource = "sys_t_user.ashx";
                //log.createdBy = base.UserLoggedIn;
                //log.createdDate = DateTime.Now;
                //logDal.Save(log);
            }

            // send params into jqGrid's format
            var gridData = new
            {
                page = page,
                total = totalPages,
                records = totalCount,
                rows = pagedList,

            };

            return Newtonsoft.Json.JsonConvert.SerializeObject(gridData);
        }

        #endregion

    
    }
}
